package com.wjj.happytime.fragment.main_frg;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.wjj.happytime.R;
import com.wjj.happytime.adapter.listen_frg_adapter.ListenFrgMainAdapter;
import com.wjj.happytime.application.BaseApplication;
import com.wjj.happytime.entity.listen_entity.ListenHomePage;
import com.wjj.happytime.my_interface.HttpService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ListenFragment extends Fragment implements Callback<ListenHomePage> {



    public RecyclerView rvListen;
    public ListenFrgMainAdapter adapter;
    public HttpService httpService;

    public ListenFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_listen, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //获取数据源
        getData();
        rvListen = ((RecyclerView) view.findViewById(R.id.rv_listen_main));


    }

  /**
   * 获取数据源
   */
  private void getData() {
      httpService = ((BaseApplication) getActivity().getApplication()).getService();
      httpService.getListenHomePageData().enqueue(this);
  }

    @Override
    public void onResponse(Call<ListenHomePage> call, Response<ListenHomePage> response) {
        adapter = new ListenFrgMainAdapter(getContext(),response.body());
        rvListen.setAdapter(adapter);
    }

    @Override
    public void onFailure(Call<ListenHomePage> call, Throwable t) {

        Toast.makeText(getContext(), "获取数据失败，请检查您的网络！", Toast.LENGTH_SHORT).show();
    }
}
